<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
          name="viewport">
    <link rel="stylesheet" href="/asset/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card-body">
                <div class="layui-form" lay-filter="form">

                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名<span style="color: red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="username" autocomplete="off" lay-verify="required">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码<span style="color: red">*</span></label>
                        <div class="layui-input-block">
                            <input type="password" class="layui-input" name="password" autocomplete="off" lay-verify="required">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">昵称</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="nickname" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="phone" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="email" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">年龄</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="age" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <select name="sex">
                                <option value="0">男</option>
                                <option value="1">女</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">住址</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="address" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">真实姓名</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="realName" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">身份证号</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="idCardNumber" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">账号状态</label>
                        <div class="layui-input-block">
                            <input type="radio" name="status" value="0" title="正常" checked>
                            <input type="radio" name="status" value="1" title="停用">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">部门</label>
                        <div class="layui-input-block">
                            <div id="dept"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">岗位</label>
                        <div class="layui-input-block">
                            <div id="post"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block">
                            <div id="role"></div>
                        </div>
                    </div>
                    <div class="layui-form-item" style="display: none" id="form-item-tenant">
                        <label class="layui-form-label">租户</label>
                        <div class="layui-input-block">
                            <div id="tenant"></div>
                        </div>
                    </div>

                    <div class="layui-form-item layui-hide">
                        <input type="button" lay-submit lay-filter="save" id="save">
                        <input type="button" lay-submit lay-filter="update" id="update">
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="/asset/layui/layui.js"></script>
<script src="/my/fast.js"></script>
<script src="/asset/modules/xm-select.js"></script>
<script>
    // 接口地址
    let saveOrUpdate = UPMS_ADDRESS + '/sysUser/mySaveOrUpdate';
    let get = UPMS_ADDRESS + '/sysUser/get';

    let sysDept_deptTree2 = UPMS_ADDRESS + '/sysDept/deptTree2';

    let sysPost_listPosts = UPMS_ADDRESS + '/sysPost/listPosts';
    let sysUserPost_listPostIdsByUserId = UPMS_ADDRESS + '/sysUserPost/listPostIdsByUserId';
    let sysUserPost_addPosts = UPMS_ADDRESS + '/sysUserPost/addPosts';

    let sysRole_listRoles = UPMS_ADDRESS + '/sysRole/listRoles';
    let sysUserRole_listRoleIdsByUserId = UPMS_ADDRESS + '/sysUserRole/listRoleIdsByUserId';
    let sysUserRole_addRoles = UPMS_ADDRESS + '/sysUserRole/addRoles';

    let sysTenant_listTenants = UPMS_ADDRESS + '/sysTenant/listTenants';
    let sysTenant_initNewTenantDBDataByUserIdAndTenantId = UPMS_ADDRESS + '/sysTenant/initNewTenantDBDataByUserIdAndTenantId';

    layui.use(() => {
        let $ = layui.$;
        let form = layui.form;

        let loginId = getItemLocalStorage('userId');
        // 当登录用户的id为1，并且是新增用户时，显示租户下拉框
        if (loginId === 1 && !oneValue()) {
            $('#form-item-tenant').show();
        }

        // 渲染部门下拉框
        let dept;
        let depts;
        xmSelectRender($, '#dept', 1, sysDept_deptTree2, 'post', (obj, data) => {
            dept = obj;
            depts = data;
        });

        // 渲染岗位下拉框
        let post;
        let posts;
        xmSelectRender($, '#post', 0, sysPost_listPosts, 'get', (obj, data) => {
            post = obj;
            posts = data;
        });

        // 渲染角色下拉框
        let role;
        let roles;
        xmSelectRender($, '#role', 0, sysRole_listRoles, 'get', (obj, data) => {
            role = obj;
            roles = data;
        });

        // 渲染租户下拉框
        let tenant;
        let tenants;
        xmSelectRender($, '#tenant', -1, sysTenant_listTenants, 'get', (obj, data) => {
            tenant = obj;
            tenants = data;
        });

        function submit(data) {
            // 部门操作
            let selectedDepts = dept.getValue();
            let selectedDeptIds = [];
            selectedDepts.forEach(v => {
                selectedDeptIds.push(v.value);
            });
            if (selectedDeptIds.length > 0) {
                data.deptId = selectedDeptIds[0];
            } else {
                data.deptId = null;
            }
            // 岗位操作
            let selectedPosts = post.getValue();
            let selectedPostIds = [];
            selectedPosts.forEach(v => {
                selectedPostIds.push(v.value);
            });
            // 角色操作
            let selectedRoles = role.getValue();
            let selectedRoleIds = [];
            selectedRoles.forEach(v => {
                selectedRoleIds.push(v.value);
            });
            // 租户操作
            let selectedTenants = tenant.getValue();
            let selectedTenantIds = [];
            selectedTenants.forEach(v => {
                selectedTenantIds.push(v.value);
            });
            if (selectedTenantIds.length > 0) {
                data.tenantId = selectedTenantIds[0];
            } else {
                data.tenantId = null;
            }

            // 是否能添加岗位或角色
            let canAddPostsOrAddRoles = true;
            layuiSubmitAsPostBody(saveOrUpdate, data, 'table', rs => {
                let userId = rs.data;
                // 当登录用户的id为1，并且是新增用户时，才可以操作租户下拉框
                if (loginId === 1 && !oneValue()) {
                    // 根据用户id、租户id，初始化新租户数据库数据
                    if (userId && data.tenantId && data.tenantId !== '1') {
                        canAddPostsOrAddRoles = false;
                        $.post(sysTenant_initNewTenantDBDataByUserIdAndTenantId, {userId: userId, tenantId: data.tenantId});
                    }
                }
                if (canAddPostsOrAddRoles) {
                    $.post(sysUserPost_addPosts, {userId: userId, postIds: selectedPostIds.toString()});
                    $.post(sysUserRole_addRoles, {userId: userId, roleIds: selectedRoleIds.toString()});
                }
                // 重载表格
                parent.layui.table.reload('table');
                // 关闭子页面
                layuiCloseChildIframe();
            });

        }

        // 监听表单提交--save
        form.on('submit(save)', data => {
            submit(data.field);
        });

        // 更新时为表单赋值
        let id = oneValue();

        if (id) {
            $.get(get, {id: id}, rs => {
                if (rs.success) {
                    let data = rs.data;
                    // 为表单赋值
                    form.val('form', data);
                    // 监听表单提交--update
                    form.on('submit(update)', obj => {
                        submit({...data, ...obj.field});
                    });

                    // 为部门下拉框动态赋值
                    if (depts) {
                        if (data.deptId) {
                            dept.append(data.deptId.split());
                        }
                    }
                    // 为岗位下拉框动态赋值
                    if (posts) {
                        $.get(sysUserPost_listPostIdsByUserId, {userId: id}, rs => {
                            if (rs.success) {
                                post.append(rs.data);
                            }
                        });
                    }
                    // 为角色下拉框动态赋值
                    if (roles) {
                        $.get(sysUserRole_listRoleIdsByUserId, {userId: id}, rs => {
                            if (rs.success) {
                                role.append(rs.data);
                            }
                        });
                    }
                    // 为租户下拉框动态赋值
                    if (tenants) {
                        if (data.tenantId) {
                            tenant.append(data.tenantId.split());
                        }
                    }

                }
            });
        }

    });
</script>
</body>
</html>
